using System;
using System.Collections;
using System.Collections.Generic;

namespace MartinRL.ProjectEuler.Sequences
{
    public class TriangelNumberSequence : IEnumerable<int>
    {
        private TriangelNumberSequence() {}

        public static IEnumerable<int> NewSequence()
        {
            return new TriangelNumberSequence();
        }

        public IEnumerator<int> GetEnumerator()
        {
            return new TriangleNumberEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public class TriangleNumberEnumerator : IEnumerator<int>
        {
            private int _count = 0;
            private int _currentNumber = 0;
            private int _lastNumber = 0;

            public void Dispose() { }

            public bool MoveNext()
            {
                _lastNumber = _currentNumber;
                _count++;
                _currentNumber = _lastNumber + _count;

                return true;
            }

            public void Reset()
            {
                throw new NotSupportedException();
            }

            public int Current
            {
                get { return _currentNumber; }
            }

            object IEnumerator.Current
            {
                get { return Current; }
            }
        }
    }
}